/**
 * Created by DukeCrushIt on 2015/8/4.
 */
class DiamondElement extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.DiamondElementSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.updateDisplay, this);
    }
    private img_add:eui.Image;
    //private diamond_group:eui.Group;
    private img_add_1:eui.Image;
    private hair_group_1:eui.BitmapLabel;
    private img_add_2:eui.Image;
    private hair_group_2:eui.BitmapLabel;
    public img_money_0:eui.Image;
    public img_money_1:eui.Image;
    public lab_diamond:eui.BitmapLabel;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if( name == "img_add"){
            this.img_add = instance;
            this.img_add.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdd, this);
        }else if( name == "img_add_1"){
            this.img_add_1 = instance;
            this.img_add_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdd, this);
        }else if( name == "hair_group_1"){
            this.hair_group_1 = instance;
        }else if( name == "img_add_2"){
            this.img_add_2 = instance;
            this.img_add_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdd, this);
        }else if( name == "hair_group_2"){
            this.hair_group_2 = instance;
        }else if( name == "img_money_0"){
            this.img_money_0 = instance;
            this.img_money_0.source = Model.me.myType == Model.GOOD_TYPE_CAT ? "img_money_cat" : "img_money_dog";
        }else if( name == "img_money_1"){
            this.img_money_1 = instance;
            this.img_money_1.source = Model.me.myType == Model.GOOD_TYPE_CAT ? "img_money_dog" : "img_money_cat";
        }else if( name == "lab_diamond"){
            this.lab_diamond = instance;
        }
    }

    private onAdd(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( evt.target == this.img_add){
            //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
            BuyDiamondPanel.getInstance().show();
        }else if( evt.target == this.img_add_1){
            BuffPanel.getInstance().show(2);
        }else if( evt.target == this.img_add_2){
            BuffPanel.getInstance().show(2);
        }

    }
    private curDiamonNum:number;
    private hair1Num:number;
    private hair2Num:number;
    public updateDisplay(evt:egret.Event=null){
        var num = Model.me.diamond;
        if( num  !=  this.curDiamonNum){
            //GetUIAssetsNum(Model.me.diamond,this.diamond_group,1.0,true,false,null,"preload_json.num_",true);
            this.lab_diamond.text = GetNumString(Model.me.diamond,false,true);
            this.curDiamonNum = num;
        }
        var num1:number;
        if(Model.me.myType == Model.GOOD_TYPE_CAT){
            num = Model.me.catHair + Model.TEMP_CAT_HAIRS;
            num1 = Model.me.dogHair + Model.TEMP_DOG_HAIRS;
        }else{
            num = Model.me.dogHair + Model.TEMP_DOG_HAIRS;
            num1 = Model.me.catHair + Model.TEMP_CAT_HAIRS;
        }
        if( this.hair1Num != num){
            this.hair_group_1.text = GetNumString(num,false,true);
            //GetUIAssetsNum(num,this.hair_group_1,1.0,true,false,null,"preload_json.num_",true);
            this.hair1Num = num;
        }
        if( this.hair2Num != num1){
            this.hair_group_2.text = GetNumString(num1,false,true);
            //GetUIAssetsNum(num1,this.hair_group_2,1.0,true,false,null,"preload_json.num_",true);
            this.hair2Num = num1;
        }
    }

    private static _instance:DiamondElement;
    public static getInstance():DiamondElement{
        if( DiamondElement._instance == null)
            DiamondElement._instance = new DiamondElement();
        return DiamondElement._instance;
    }
}